package com.jams.goodox.activity.serve.headhunter;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.entity.ChooseItem;
import com.jams.goodox.entity.JobBean;
import com.jams.goodox.entity.resp.UserJobListResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HeadHunterService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;
import com.jams.library.widget.RecyclerDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * create by jams on 2017/06/06
 * <p>
 * 个人职位列表页面的职位搜索
 */
public class JobListSearchActivity extends BaseActivity {

    @Bind(R.id.et_job_search)
    EditText etJobSearch;
    @Bind(R.id.rcv_job_search)
    RefreshRecyclerView rcvJobSearch;
    @Bind(R.id.empty_view)
    LoadView emptyView;

    private List<JobBean> dataList;
    private int curPage;
    private List<ChooseItem> yearList, educationList, priseList;
    private String key;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_job_list_search;
    }

    @Override
    protected void init() {
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.search_gray));//沉侵式头部
        setTitleVisibility(false);
        dataList = new ArrayList<>();
        yearList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.year_need), -1);
        educationList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.education_need), -1);
        priseList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.prise_need), -1);
        rcvJobSearch.setLayoutManager(new RefreshLinearLayoutManager(this));
        rcvJobSearch.setAdapter(new RecycleCommonAdapter<JobBean>(this, dataList, R.layout.job_list_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final JobBean item, int position) {
                helper.setSimpleDraweeView(R.id.sdv_job, item.getEnterpriseLogo())
                        .setText(R.id.tv_job_name, item.getJobName())
                        .setText(R.id.tv_job_price, priseList.get(item.getSalary()).getName())
                        .setText(R.id.tv_job_company, item.getEnterpriseName())
                        .setText(R.id.tv_job_need, yearList.get(item.getJobLifeRequire()).getName() + " " + educationList.get(item.getEducationRequire()).getName())
                        .setText(R.id.tv_recommend_money, StringUtils.doubleToIntegerString(item.getReward()) + "元")
                        .setText(R.id.tv_recommend_time, item.getCreateTime())
                        .setOnItemClickListener(new OnRecycleItemClickListener() {
                            @Override
                            public void itemClick() {
//                                Intent intent = new Intent(JobListSearchActivity.this, JobInfoDetailActivity.class);
//                                intent.putExtra(JobInfoDetailActivity.ID, item.getId());
//                                qStartActivity(intent);
                            }
                        });
            }
        });
        etJobSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    curPage = 1;
                    key = etJobSearch.getText().toString();
                    search();
                }
                return false;
            }
        });
        rcvJobSearch.addItemDecoration(new RecyclerDivider(this, LinearLayoutManager.VERTICAL, 2, ContextCompat.getColor(this, R.color.gray_background)));
        rcvJobSearch.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                search();
            }
        });
    }

    private void search() {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        showLoading();
        enqueue(createCallService(HeadHunterService.class).getUserJobList(key, curPage, AppConfig.PagSize), new RetrofitCallbackListener<UserJobListResp>() {
            @Override
            public void onSuccess(Call<UserJobListResp> call, UserJobListResp result) {
                misLoading();
                rcvJobSearch.loadMoreComplete();
                if (result.getData() != null && result.getData().getResultList() != null) {
                    if (curPage == 1) {
                        dataList.clear();
                    }
                    if (result.getData().getPageInfo() == null) {
                        rcvJobSearch.setCanLoadMore(false);
                    } else {
                        rcvJobSearch.setCanLoadMore(result.getData().getPageInfo().isHasMore());
                    }
                    dataList.addAll(result.getData().getResultList());
                    curPage++;
                    rcvJobSearch.notifyData();
                }
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_job,R.string.search_none);
                } else {
                    emptyView.loadComplete();
                }
            }

            @Override
            public void onError(String msg) {
                misLoading();
                rcvJobSearch.loadMoreComplete();
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_job,R.string.search_none);
                } else {
                    emptyView.loadComplete();
                }
            }
        });
    }

    @OnClick(R.id.tv_cancel_search)
    public void onViewClicked() {
        onBackPressed();
    }
}
